package cn.com.pc.content.domain.tag;

import cn.com.pc.content.domain.Biz;
import cn.com.pc.content.domain.Site;
import cn.com.pc.content.jpa.BizConverter;
import cn.com.pc.content.jpa.SiteConverter;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import lombok.Data;
import lombok.ToString;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 标签导航树，实现树型导航
 */
@Data
@ToString
@Entity
@Table(name = "tag_trees")
@DynamicUpdate
@DynamicInsert
public class TagTree {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

//    与子标签树列表产生递归循环
//    @ManyToOne
//    @JoinColumn()
//    private TagTree parent;

    /**
     * 父标签树id
     */
    @Column(name = "parent_id")
    private Long parentId;

    /**
     * 站点
     */
    @Column(name = "site")
    @Convert(converter = SiteConverter.class)
    private Site site;

    /**
     * 业务
     */
    @Column(name = "biz")
    @Convert(converter = BizConverter.class)
    private Biz biz;

    /**
     * 标签树名
     */
    @Column(name = "name")
    private String name;

    /**
     * 创建时间
     */
    @JsonFormat(timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss", shape = JsonFormat.Shape.STRING)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @Column(name = "created_at")
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @JsonFormat(timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss", shape = JsonFormat.Shape.STRING)
    @JsonSerialize(using = LocalDateTimeSerializer.class)
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    /**
     * 子标签树列表
     */
    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumn(name = "parent_id")
    private List<TagTree> subTree;

//    /**
//     * 标签列表
//     */
//    @OneToMany(fetch = FetchType.LAZY)
//    @JoinColumn(name = "tree_id")
//    private List<Tag> tags;
}
